package com.cea.papyrus.core.actions.selectcopypaste;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;

import com.cea.papyrus.core.commands.copypaste.CopyToClipboardCommand;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.treeeditpart.ElementTreeEditPart;

/**
 * An action to copy selected objects.
 */
public class CopyAction extends SelectionAction {

	/** standard ID of the action in the workbench */
	public static final String ID = ActionFactory.COPY.getId();

	/**
	 * @param editor The editor this action will be associated with.
	 */
	public CopyAction(IEditorPart editor) {
		this((IWorkbenchPart)editor);
	}

	/**
	 * Creates a <code>CopyAction</code> with the given label.
	 * @param editor The editor this action will be associated with.
	 * @param label  The label to be displayed for this action.
	 */
	public CopyAction(IEditorPart editor, String label) {
		this((IWorkbenchPart)editor);
		setText(label);
	}

	/**
	 * Constructs a <code>CopyAction</code> using the specified part.
	 * @param part The part for this action
	 */
	public CopyAction(IWorkbenchPart part) {
		super(part);
		setLazyEnablementCalculation(false);
	}

	/**
	 * Returns <code>true</code> if the selected objects can
	 * be deleted.  Returns <code>false</code> if there are
	 * no objects selected or the selected objects are not
	 * {@link EditPart}s.
	 * @return <code>true</code> if the command should be enabled
	 */
	protected boolean calculateEnabled() {
		Command cmd = createCopyCommand(getSelectedObjects());
		if (cmd == null)
			return false;
		return cmd.canExecute();
	}

	/**
	 * Create a command to copy the selected objects.
	 * @param objects The objects to be copied.
	 * @return The command to copy the selected objects.
	 */
	public Command createCopyCommand(List objects) {
		Iterator it= getSelectedObjects().iterator();

		Collection umlElements = new ArrayList();
		while(it.hasNext()) {
			Object o = it.next();
			if(o instanceof ElementTreeEditPart) {
				umlElements.add(((ElementTreeEditPart)o).getUmlElement());
			}
		}
		if(umlElements.size() > 0) {
			ModelManager modelManager = (ModelManager)getWorkbenchPart().getAdapter(ModelManager.class);
			if(modelManager != null) {
				return new CopyToClipboardCommand(umlElements, modelManager);
			}
		}
		return UnexecutableCommand.INSTANCE;
	}

	 /* (non-Javadoc)
	 * @see org.eclipse.gef.ui.actions.SelectionAction#setSelection(org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void setSelection(ISelection selection) {
		// overrides for visibility
		super.setSelection(selection);
	}
	
	/**
	 * Initializes this action's text and images.
	 */
	protected void init() {
		super.init();
		setText("Copy");
		setToolTipText("Copy selected elments into clipboard");
		setId(ActionFactory.COPY.getId());
		ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
		setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
		setDisabledImageDescriptor(sharedImages.getImageDescriptor(
				ISharedImages.IMG_TOOL_COPY_DISABLED));
		setEnabled(false);
	}

	/**
	 * Performs the copy action on the selected objects.
	 */
	public void run() {
		execute(createCopyCommand(getSelectedObjects()));
	}

}
